<!--
 * @Author       : xiaowu wwl093000@163.com
 * @Date         : 2023-07-21 08:41:00
 * @LastEditTime : 2023-07-27 15:13:45
 * @LastEditors  : xiaowu wwl093000@163.com
 * @FilePath     : /erp-admin-ui/src/views/warehouse/storage-list.vue
 * @Description  : -仓储列表
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品编码：" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择仓库：" prop="warehouseId">
        <el-select
          v-model="queryParams.warehouseId"
          placeholder="请选择仓库"
          clearable
        >
          <el-option
            v-for="dict in warehouseList"
            :key="dict.warehouseName"
            :label="dict.warehouseName"
            :value="dict.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >申请仓储
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="warehousInfoList">
      <el-table-column label="商品编码" align="center" prop="skuCode"/>
      <el-table-column label="商品名称" align="center" prop="skuName" width="300"/>
      <el-table-column label="商品主图" align="center" prop="skuImg">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.skuImg" width="50px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{
            scope.row.status == '0' ? '在途' : scope.row.status == '1' ? '入库' : scope.row.status == '2' ? '出库' : ''
          }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="number"/>
      <el-table-column label="所在仓库" align="center" prop="warehouseName"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改FPX仓储信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品编码" prop="skuCode">
          <el-input v-model="form.skuCode" placeholder="请输入商品编码"/>
        </el-form-item>
        <el-form-item label="商品数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="选择仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择仓库">
            <el-option
              v-for="dict in warehouseList"
              :key="dict.warehouseName"
              :label="dict.warehouseName"
              :value="dict.warehouseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="国内运单号" prop="cnTrackingNo">
          <el-input v-model="form.cnTrackingNo" placeholder="请输入国内运单号"/>
        </el-form-item>
        <el-form-item label="购买凭证：" prop="proof">
          <image-upload
            v-model="form.proof"
            :is-show-tip="false"
            width="50px"
            height="50px"
          />
        </el-form-item>
        <el-form-item label="预计到货时间" prop="arrivalTime">
          <el-date-picker
            clearable
            v-model="form.arrivalTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计到货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采购成本" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购成本"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWarehousInfo,
  addWarehousInfo,
  getProductCode,
  warehouseList
} from "@/api/warehouse/warehouse";

export default {
  name: "StorageList",
  dicts: ['erp_currency_code'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // FPX仓储信息表格数据
      warehousInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skuCode: undefined,
        warehouseId: undefined
      },
      // 表单参数
      form: {
        type: '1'
      },
      // 表单校验
      rules: {
        skuCode: [{required: true, message: "商品编码不能为空", trigger: "blur"}],
        number: [{required: true, message: "商品数量不能为空", trigger: "blur"}],
        warehouseId: [{required: true, message: "仓库不能为空", trigger: ["blur", "change"]}],
        cnTrackingNo: [{required: true, message: "国内运单号不能为空", trigger: "blur"}],
        proof: [{required: true, message: "购买凭证不能为空", trigger: "blur"}],
        arrivalTime: [{required: true, message: "预计到货时间不能为空", trigger: ["blur", "change"]}],
        purchasePrice: [{required: true, message: "采购成本不能为空", trigger: "blur"}],
      },
      warehouseList: [] // 仓库列表
    };
  },
  created() {
    // this.getWarehouseList();
    this.getList();
  },
  methods: {
    getProductCode() {
      getProductCode().then(response => {
        this.warehousInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询FPX仓储信息列表 */
    getList() {
      this.loading = true;
      listWarehousInfo(this.queryParams).then(response => {
        this.warehousInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取仓库列表
    getWarehouseList() {
      warehouseList().then(response => {
        this.warehouseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        skuCode: undefined,
        number: undefined,
        warehouseId: undefined,
        cnTrackingNo: undefined,
        proof: undefined,
        arrivalTime: undefined,
        purchasePrice: undefined,
        type: '1'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加FPX仓储信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let time = new Date(this.form.arrivalTime)
          this.form.arrivalTime = time.getTime()
          this.buttonLoading = true;
          addWarehousInfo(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warehousInfo/export', {
        ...this.queryParams
      }, `warehousInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
